package com.example.ui.learn.util;
import java.util.ArrayList;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;

import com.example.ui.learn.CurriculumCenterActivity;
import com.example.ui.learn.ListeningActivity;
import com.example.ui.learn.ListeningInfo;
import com.example.util.AsyncLoadImage;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
//my class room
public class MyListViewAdapter3 extends BaseAdapter {

	private LayoutInflater inflater;
	private ArrayList<MyClassroom> list;
	private AsyncLoadImage asyncLoadImage;
	
	private String id, title, name, thumb,blID;
	private int tag;

	public MyListViewAdapter3(Context mContext, ArrayList<MyClassroom> list,
			AsyncLoadImage ali) {
		inflater = LayoutInflater.from(mContext);
		this.list = list;
		this.asyncLoadImage = ali;
	}

	public ArrayList<MyClassroom> getCcInfos() {
		return list;
	}

	public void setCcInfos(ArrayList<MyClassroom> list) {
		this.list = list;
		if (list == null) {
			this.list = new ArrayList<MyClassroom>();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;

		MyClassroom mc = list.get(position);
		final String url = mc.getPic();

		final ImageView imageView;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item02_item, null);
			holder = new Holder();

			holder.textTitle = (TextView) convertView
					.findViewById(R.id.i2i_title);
			holder.mark = (ImageView) convertView.findViewById(R.id.i2i_mark);
			
			holder.teacher = (TextView) convertView
					.findViewById(R.id.i2i_teacher);
			holder.listen = (TextView) convertView
					.findViewById(R.id.i2i_listen);

			convertView.setTag(holder);

		} else {

			holder = (Holder) convertView.getTag();
		}
		imageView = (ImageView) convertView.findViewById(R.id.i2i_pic);
		imageView.setTag(url);
		asyncLoadImage.imageLoader.displayImage(url, imageView,asyncLoadImage.options);
		imageView.setTag(url);
		tag = position;
		holder.mark.setTag(tag);
		holder.mark.setOnClickListener(new OnClickListener() {

			private ListeningInfo listenInfo=new ListeningInfo();
			@Override
			public void onClick(View v) {
				tag = (Integer) v.getTag();
				id = list.get(tag).getId();
				thumb = list.get(tag).getPic();
				title = list.get(tag).getTitle();
				name = list.get(tag).getName();
				blID=list.get(tag).getBlId();
				
				Intent i1_l = new Intent();
				i1_l.setClass(inflater.getContext(), ListeningActivity.class);
				if(HSApplication.HSlistInfo.getListen_uri()!=null&&!"".equals(HSApplication.HSlistInfo.getListen_uri())){
					if(HSApplication.HSlistInfo.getListen_itemId().equals(id)){
						listenInfo=HSApplication.HSlistInfo;
						i1_l.putExtra(ListeningActivity.ISLISTENING,true);
					}else{
						listenInfo=new ListeningInfo("", thumb,title,name,blID, 1, id,"",-1,-1,0);
						i1_l.putExtra(ListeningActivity.ISLISTENING,false);
					};
				}else{
					listenInfo=new ListeningInfo("", thumb,title,name,blID, 1, id,"",-1,-1,0);
					i1_l.putExtra(ListeningActivity.ISLISTENING,false);
				}
				Bundle lisinfo=new Bundle();
				lisinfo.putParcelable(ListeningActivity.LISTEN,listenInfo);
				i1_l.putExtras(lisinfo);
				i1_l.putExtra("intro",true);
				inflater.getContext().startActivity(i1_l);
			}
		});
		holder.textTitle.setText(mc.getTitle());
		holder.teacher.setText(mc.getName());
		holder.listen.setText("共" + mc.getTotle() + "讲\t" + "已听"
				+ mc.getListen() + "讲");

		return convertView;
	}

	class Holder {
		ImageView pic;
		TextView teacher;
		TextView listen;
		TextView textTitle;
		ImageView mark;
		

	}


}
